package model.altarica_fields;

import java.util.*;
import model.AltaRicaNode;

public class Var {

    private String id;
    private List<String> subs;
    private VarType type;
    private VarType syntaxType;
    private String attribute;
    private boolean stateVar;
    private Var remoteVar;
    private AltaRicaNode node;
    private LinkedList<AltaRicaNode> remoteNodes;
    public static final String ATTR_PUBLIC = "PUBLIC";
    public static final String ATTR_PRIVATE = "PRIVATE";
    public static final String ATTR_PARENT = "PARENT";

    public Var(String id, VarType type, boolean stateVar, String attribute) {

        this.id = id;
        this.subs = null;
        this.type = type;
        this.syntaxType = type;
        
        this.stateVar = stateVar;
        this.attribute = attribute;
        this.remoteVar = null;

        this.node = null;
        this.remoteNodes = new LinkedList<AltaRicaNode>();
    }

    public Var(List<String> ids, VarType type, boolean stateVar, String attribute) {

        this.id = ((ArrayList<String>) ids).get(ids.size() - 1);

        ids.remove(ids.size() - 1);

        this.subs = null;

        if (ids.isEmpty() == false) {
            this.subs = new LinkedList<String>();
        }

        for (String str : ids) {
            ((LinkedList<String>) subs).addLast(str);
        }

        this.type = type;
        this.syntaxType = type;
        
        this.stateVar = stateVar;
        this.attribute = attribute;
        this.remoteVar = null;

        this.node = null;
        this.remoteNodes = new LinkedList<AltaRicaNode>();
    }

    public String getId() {

        String str = new String();

        if (subs != null) {
            for (String node : subs) {
                str += node + ".";
            }
        }

        str += this.id;

        return str;
    }

    public String getName() {

        return this.id;
    }

    public void setNode(AltaRicaNode node) {

        this.node = node;
    }

    public AltaRicaNode getNode() {

        return this.node;
    }

    public void addRemoteReference(AltaRicaNode rnode) {

        this.remoteNodes.add(rnode);
    }

    public Collection<AltaRicaNode> getRemoteReference() {

        return Collections.unmodifiableCollection(this.remoteNodes);
    }

    public boolean isStateVar() {

        return this.stateVar;
    }

    public boolean isPublic() {

        return this.attribute.equals(ATTR_PUBLIC);
    }

    public boolean isPrivate() {

        return this.attribute.equals(ATTR_PRIVATE);
    }

    public boolean isParent() {

        return this.attribute.equals(ATTR_PARENT);
    }

    public boolean isLocal() {

        return this.subs == null;
    }

    public List<String> getSubs() {

        return this.subs;
    }

    public void setRemoteVar(Var var) {

        this.remoteVar = var;

        this.type = var.type;
        this.attribute = var.attribute;
        this.stateVar = var.stateVar;
    }

    public Var getRemoteVar() {

        return this.remoteVar;
    }

    public VarType getType() {

        return this.type;
    }

    public void setType(VarType type) {

        this.type = type;
    }

    @Override
    public String toString() {

        String str = new String();

        if (subs != null) {
            for (String node : subs) {
                str += node + ".";
            }
        }

        str += this.id;

        if (syntaxType != null) {
            str += " : " + syntaxType.toString() + " : " + attribute;
        }

        return str;
    }
}
